<template>
  <div class="page-continue">
    <el-tabs v-model="activeName" @tab-click="handleClick">
      <el-tab-pane label="修改密码" name="1"></el-tab-pane>
      <el-tab-pane label="修改头像" name="2"></el-tab-pane>
      <el-tab-pane label="修改昵称" name="3"></el-tab-pane>
    </el-tabs>
    <div v-if="activeName==1">
      <el-form ref="form" :model="form1" label-width="80px">
        <el-form-item label="密码">
          <el-input v-model="form1.password"></el-input>
        </el-form-item>
        <el-form-item label="重复密码">
          <el-input v-model="form1.againpassword"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="changePass">修改密码</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div v-if="activeName==2">
      修改头像
      <el-upload
        class="avatar-uploader"
        action="/api/user/file"
        :show-file-list="false"
        :on-success="handleAvatarSuccess"
        :before-upload="beforeAvatarUpload"
      >
        <img v-if="imageUrl" :src="imageUrl" class="avatar">
        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
      </el-upload>
    </div>
    <div v-if="activeName==3">
      <el-form ref="form" :model="form2" label-width="80px">
        <el-form-item label="昵称">
          <el-input v-model="form2.nickname"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="changeName">修改昵称</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import userApi from "@/assets/api/user.js";
export default {
  data() {
    return {
      activeName: "1",
      imageUrl: "",
      form1: {
        password: null,
        againpassword: null
      },
      form2: {
        nickname: null
      }
    };
  },
  methods: {
    changePass() {
      if (this.form1.password == this.form1.againpassword)
        userApi.changePass(this.form1.password).then(res => {
          if (res.data.success == 1)
            this.$message({
              type: "success",
              message: "成功"
            });
          else
            this.$message({
              type: "warning",
              message: res.data.message
            });
        });
      else
        this.$message({
          type: "warning",
          message: "两次密码不一致"
        });
    },
    handleAvatarSuccess(res) {
      this.imageUrl = res.url;
      userApi.changeHeadimg(this.imageUrl).then(res => {
        if (res.data.success == 1)
          this.$message({
            type: "success",
            message: "成功"
          });
      });
    },
    changeName() {
      userApi.changeNickname(this.form2.nickname).then(res => {
        if (res.data.success == 1)
          this.$message({
            type: "success",
            message: "成功"
          });
        else
          this.$message({
            type: "warning",
            message: res.data.message
          });
      });
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    }
  }
};
</script>

<style scoped>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>